package cn.itcast.web.controller.cargo;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.util.DownloadUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping(value = "/cargo/invoice",name = "发票管理")
public class InvoiceController extends BaseController {
    @Reference
    private InvoiceService invoiceService;

    @Reference
    PackingService packingService;

    @Reference
    private ShippingService shippingService;

    @RequestMapping(value = "/list",name ="委托分页查询" )
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){

        InvoiceExample invoiceExample = new InvoiceExample();
        PageInfo page = invoiceService.findByPage(pageNum,pageSize,invoiceExample);
        request.setAttribute("page",page);
        return "cargo/invoice/invoice-list";
    }

    @RequestMapping(value = "/toUpdate",name = "去修改页面")
    public String toUpdate(String id){
        Invoice invoice = invoiceService.selectByPrimaryKey(id);
        System.out.println(invoice);
        request.setAttribute("invoice",invoice);
        return "cargo/invoice/invoice-update";
    }

    @RequestMapping("/toInvoice")
    public String toInvoice(String id){

        Invoice invoice = new Invoice();
        invoice.setCreateBy(getLoginUser().getUserName());
        invoice.setCreateDept(getLoginUser().getDeptName());
        invoice.setCreateTime(new Date());
        invoiceService.save(invoice,id);
        return "redirect:/cargo/invoice/list.do";
    }
    @RequestMapping(value = "/edit",name = "新增或修改")
    public String edit(Invoice invoice,String shippingOrderId){
        Shipping shipping = shippingService.selectByPrimaryKey(shippingOrderId);
        shipping.setState(2);
        shippingService.update(shipping);

        invoice.setCreateBy(getLoginUser().getUserName());
        invoice.setCreateDept(getLoginUser().getDeptName());
        invoice.setCreateTime(new Date());
        if (StrUtil.isEmpty(invoice.getInvoiceId())) { // 新增报运单
            invoiceService.save(invoice,shippingOrderId);
        }else{ //更新报运单
            invoiceService.update(invoice);
        }
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/delete",name = "删除")
    public String delete(String id){
        invoiceService.deleteByPrimaryKey(id);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/submit",name = "提交")
    public String submit(String id){
        Invoice invoice = invoiceService.selectByPrimaryKey(id);
        invoice.setState(1);
        invoiceService.update(invoice);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/cancel",name = "取消")
    public String cancel(String id){
        Invoice invoice = invoiceService.selectByPrimaryKey(id);
        invoice.setState(0);
        invoiceService.update(invoice);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/printExcelWithTemplate", name = "excel模板下载")
    public void printExcelWithTemplate(String id) throws Exception {
        Invoice invoice = invoiceService.selectByPrimaryKey(id);
        Packing packing = packingService.selectByPrimaryKey(id);


        // ii.使用easyPOI将通过模板将数据封装为Workbook
        // 1.加载模板
        String realPath = session.getServletContext().getRealPath("make/xlsprint/7-发票.xls");// 获取模板真实路径
        TemplateExportParams params = new TemplateExportParams(realPath);
        // 2.准备数据（模型）
        Map map = new HashedMap();
        map.put("title", "发票");
        map.put("invoiceId",invoice.getInvoiceId());
        map.put("seller", packing.getSeller());
        map.put("buyer", packing.getBuyer());
        map.put("scNo",invoice.getScNo());
        map.put("blNo",invoice.getBlNo());
        map.put("tradeTerms",invoice.getTradeTerms());
        map.put("createTime",invoice.getCreateTime());
        // 3.封装为workbook
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);

        // iii.使用工具类完成文件下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        DownloadUtils.download(byteArrayOutputStream, response, "发票.xlsx");

    }


}
